﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NCM_MSTest.Utils
{
    /// <summary>
    /// 多项式计算
    /// </summary>
    public class Polynomial
    {
        /// <summary>
        /// 直接法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double Compute_Direct(double[] a, double x)
        {
            double ret = 0d;
            double[] temp = new double[a.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                if (a[i] != 0)
                {
                    temp[i] = Math.Pow(x, i) * a[i];
                    ret += temp[i];
                }
                else
                    temp[i] = 0;
            }
            return ret;
        }

        public static double Compute_Iteration(double[] a, double x)
        {
            double ret = 0d;
            double r = 1;
            ret = a[0];
            for (int i = 1; i < a.Length; i++)
            {
                r = r * x;
                ret = ret + a[i] * r;
            }
            return ret;
        }

        public static double Compute_Nest(double[] a, double x)
        {
            double ret = a[a.Length - 1];
            for (int i = a.Length - 2; i > -1; i--)
            {
                ret = ret * x + a[i];
            }
            return ret;
        }
    }
}
